<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\room;


use app\common\model\room\RoomApartmentDeviceLog;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * 公寓设备记录逻辑
 * Class RoomApartmentDeviceLogLogic
 * @package app\adminapi\logic\room
 */
class RoomApartmentDeviceLogLogic extends BaseLogic
{


    /**
     * @notes 添加公寓设备记录
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            RoomApartmentDeviceLog::create([
                'cid' => $params['cid'],
                'rid' => $params['rid'],
                'device_id' => $params['device_id'],
                'coefficient' => $params['coefficient'],
                'price' => $params['price'],
                'base_number' => $params['base_number'],
                'reading' => $params['reading'],
                'use_total' => $params['use_total'],
                'lift_fee' => $params['lift_fee'],
                'second_pressure_fee' => $params['second_pressure_fee'],
                'total_fee' => $params['total_fee'],
                'log_time' => $params['log_time'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑公寓设备记录
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            RoomApartmentDeviceLog::where('id', $params['id'])->update([
                'cid' => $params['cid'],
                'rid' => $params['rid'],
                'device_id' => $params['device_id'],
                'coefficient' => $params['coefficient'],
                'price' => $params['price'],
                'base_number' => $params['base_number'],
                'reading' => $params['reading'],
                'use_total' => $params['use_total'],
                'lift_fee' => $params['lift_fee'],
                'second_pressure_fee' => $params['second_pressure_fee'],
                'total_fee' => $params['total_fee'],
                'log_time' => $params['log_time'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除公寓设备记录
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function delete(array $params): bool
    {
        return RoomApartmentDeviceLog::destroy($params['id']);
    }


    /**
     * @notes 获取公寓设备记录详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function detail($params): array
    {
        return RoomApartmentDeviceLog::findOrEmpty($params['id'])->toArray();
    }
}